package com.ywd.materials.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywd.materials.admin.entity.MaterialsWarehouseUserEntity;
import com.ywd.materials.admin.mapper.MaterialsWarehouseUserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

/**
 * YWD
 * 2025/11/11
 */
@Mapper
@Service
public class MaterialsWarehouseUserServiceImpl
        extends ServiceImpl<MaterialsWarehouseUserMapper, MaterialsWarehouseUserEntity> {

    public MaterialsWarehouseUserEntity getByUserId(Long userId) {
        LambdaQueryWrapper<MaterialsWarehouseUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsWarehouseUserEntity::getSysUserId, userId)
                .eq(MaterialsWarehouseUserEntity::getDeleted,0);
        return this.getOne(queryWrapper);
    }

    public MaterialsWarehouseUserEntity getByWarehouseId(Long warehouseId) {
        LambdaQueryWrapper<MaterialsWarehouseUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsWarehouseUserEntity::getMaterialsWarehouseId, warehouseId)
                .eq(MaterialsWarehouseUserEntity::getDeleted,0);
        return this.getOne(queryWrapper);
    }

    public Boolean saveWarehouseUser(Long userId, Long warehouseId) {
        MaterialsWarehouseUserEntity entity = new MaterialsWarehouseUserEntity();
        entity.setSysUserId(userId);
        entity.setMaterialsWarehouseId(warehouseId);
        return save(entity);
    }


    public void removeByUserId(Long userId) {
       LambdaQueryWrapper<MaterialsWarehouseUserEntity> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(MaterialsWarehouseUserEntity::getSysUserId, userId);
       remove(queryWrapper);
    }

}
